home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / gateway.idb / usr / OnRamp / lib / OnRamp.pm.z / OnRamp.pm
Encoding:
Text File  |  1997-07-30  |  11.8 KB  |  559 lines

  1. #!/usr/bin/perl5
  2. use POSIX;
  3.  
  4. $METACHARS = '[\{\}\(\)\*!~\<\>\?|\[\]\'\&\^\$\@#`\";:]';
  5. $LOGINCHARS = '[\_\-\.\{\}\(\)\*!~\<\>\?|\[\]\'\&\^\$\@#`\";:\/\s]';
  6.  
  7. @bad_filenames = ('/','/usr','/etc','/bin','/sbin','/usr','/usr/sbin');
  8.  
  9. $document_root = "/usr/ns-home/httpd-gateway/docs/webface";
  10.  
  11. # note: when a file is checked in with ci -l, the mode is affected:
  12. #    ci -l is equiv. to ci; co -l
  13. #    ci (first time only) : RCS file inherets mode EXCEPT all write
  14. #        permissions are dropped.
  15. #    co -l : working file inherets mode EXCEPT user is given write
  16. #        permission.
  17.  
  18. sub rcs_initial {
  19.     local($rcs_fqname) = $_[0];
  20.     local($rcs_name) = $_[1];
  21.     local($rcs_temp) = "/tmp/rcs_temp";
  22.     local($rcs_err) = "/tmp/rcs_err";
  23.     local($rcs_diff) = 0;
  24.     local($rcs_err) = 0;
  25.     local(@items, $com);
  26.  
  27.     $rcs_name = "/usr/WebFace/RCS/" . $rcs_name . ",v";
  28.  
  29.     system("/usr/sbin/rcsdiff $rcs_fqname $rcs_name > $rcs_temp 2>$rcs_err");
  30.  
  31.     # note: rcsdiff sends header to stderr, any difference to stdout
  32.  
  33.     open(RCS_IN, "< $rcs_err");
  34.     while(<RCS_IN>) { $rcs_err = 1; }
  35.     close(IN);
  36.  
  37.     if ($rcs_err) {
  38.         $com = "/usr/sbin/ci -l -m\"gateway 2.1: initial state\" ";
  39.         $com .= "$rcs_fqname $rcs_name > /dev/null 2>&1";
  40.         system($com);
  41.     } else {
  42.         open(RCS_IN, "< $rcs_temp");
  43.         while(<RCS_IN>) { $rcs_diff = 1; }
  44.         close(RCS_IN);
  45.  
  46.         if ($rcs_diff) {
  47.             $com = "/usr/sbin/ci -l -m\"gateway 2.1: manual edit\" ";
  48.             $com .= "$rcs_fqname $rcs_name > /dev/null 2>&1";
  49.             system($com);
  50.         }    
  51.     }
  52.     unlink $rcs_temp, $rcs_err;
  53. }
  54.  
  55. sub rcs_final {
  56.     local($rcs_fqname) = $_[0];
  57.     local($rcs_name) = $_[1];
  58.     local($comment) = $_[2];
  59.     local($com);
  60.  
  61.     $rcs_name = "/usr/WebFace/RCS/" . $rcs_name . ",v";
  62.  
  63.     $com = "/usr/sbin/ci -l -m\"gateway 2.1: $comment\" ";
  64.     $com .= "$rcs_fqname $rcs_name > /dev/null 2>&1";
  65.     system($com);
  66. }
  67.  
  68. sub button_table {
  69.   $qry = $_[0];
  70.   print "<center>\n";
  71.   print "<table border=2 cellspacing=0 cellpadding=0 width=300>\n";
  72.   print "<tr><th align=center>", $qry->submit(-name=>$_[1], -value=>$_[2]),
  73.   "<th align=center>", $qry->reset,
  74.   "<th align=center>", $qry->submit(-name=>$_[3], -value=>$_[4]),
  75.   "</tr></table>";
  76.   print "</center>";
  77. }
  78.  
  79. sub hash_modem {
  80.   local($arg) = $_[0];
  81.   local($give) = $_[1];
  82.   local($get) = $_[2];
  83.   local(@items);
  84.   open(IN_HASH,"< /usr/OnRamp/etc/modem.trans");
  85.   while(<IN_HASH>) {
  86.     @items = split(/:/);
  87.     if ($arg eq $items[$give]) {
  88.       $ret = $items[$get];
  89.       last;
  90.     }
  91.   }
  92.   close(IN_HASH);
  93.   return $ret;
  94. }
  95.  
  96. sub select_list {
  97.   local(*lst) = $_[2];
  98.   local($arg);
  99.   local($ret) = "<select name=\"$_[0]\">\n";
  100.   foreach $arg (@lst) {
  101.     if ($arg eq $_[1]) { $ret .= "<option selected> $arg\n"; }
  102.     else { $ret .= "<option> $arg\n"; }
  103.   }
  104.   $ret .= "</select>\n";
  105.   return $ret;
  106. }
  107.  
  108. sub get_config {
  109.       my $ret;
  110.     system("/etc/chkconfig $_[0]");
  111.     if ($? >> 8) { $ret = "No"; }
  112.     else { $ret = "Yes"; }
  113. }
  114.  
  115. sub check_login {
  116.   return 1 if $_[0] =~ /$METACHARS/o;
  117.   return 1 if $_[0] =~ /\s/;
  118.   return 1 if length($_[0]) > 8;
  119.   0;
  120. }
  121.  
  122. sub add_password {
  123.   local($name,$pass,$uid,$gid,$fname,$dir,$sh) = @_;
  124.  
  125.   if ($pass && $pass ne "*") {
  126.     $salt = &mksalt;
  127.     $pass = crypt($pass,$salt);
  128.   }
  129.   if (getpwnam($name)) {
  130.     return 1;
  131.   }
  132.  
  133.   if ($uid != 0) {
  134.     while (1) {
  135.       if (getpwuid($uid)) {
  136.         $uid++;
  137.         next;
  138.       }
  139.       last;
  140.     }
  141.   }
  142.   $val = "$name:$pass:$uid:$gid:$fname:$dir:$sh\n";
  143.   open(OUT,">> /etc/passwd");
  144.   print OUT $val;
  145.   close(OUT);
  146.   return 0;
  147. }    
  148.     
  149. sub mksalt {
  150.   $rand = int(rand(64*64));
  151.  
  152.   $chars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  153.   $var = $rand & 0x3f;
  154.   $salt = substr($chars,$var,1);
  155.   $rand >>= 6;
  156.   $var = $rand & 0x3f;
  157.   $salt .= substr($chars,$var,1);
  158.  
  159.   return $salt;
  160. }    
  161.  
  162. sub choice_list {
  163.   local(*list) = $_[0];
  164.   local($name) = $_[1];
  165.   local($width) = $_[2];
  166.   local($count = 0);
  167.   local($arg,$i);
  168.   local($ret) = "<tt><select name=\"$name\" size=6>\n";
  169.   local($max) = $width-2; 
  170.   foreach $arg (@list) { if (length($arg) > $max) { $max = length($arg); } }
  171.   $width = $max + 2;
  172.   if (!@list) { 
  173.     $ret .= "<option>";
  174.     for ($i=0;$i<$width;$i++) { $ret .= " "; }
  175.     $ret .= "\n";
  176.   }
  177.   foreach $arg (@list) {
  178.     $ret .= "<option> ".$arg;
  179.     if (!$count) { 
  180.       $count = 1; 
  181.       for ($i=0;$i<$width-length($arg)-1;$i++) { $ret .= " "; }
  182.     }
  183.     $ret .= "\n";
  184.   }
  185.   $ret .= "</select></tt>\n";
  186.  
  187.   return $ret;
  188. }
  189.  
  190. sub convert_ipnum {
  191.   my($targ, $arg, @flds);
  192.  
  193.   ($arg) = ($_[0] =~ /^\s*(\S*)\s*$/);
  194.  
  195.   if ($arg eq "") { $_[1] = 1; return; }
  196.  
  197.   $_[1] = 0;
  198.  
  199.   if ($arg =~ /^0x/) {
  200.     if ($arg =~ /^0[xX]([0-9a-fA-F]*)$/) {
  201.       hex($arg);
  202.     }
  203.     else {
  204.       $_[1] = 1;
  205.     } 
  206.   }
  207.   else {
  208.     @flds = split(/\./, $arg);
  209.  
  210.     if ($#flds > 3) {
  211.       $_[1] = 4;
  212.       return;
  213.     }
  214.  
  215.     $targ = 0;
  216.  
  217.     foreach (@flds) {
  218.       if (! /^\d+$/) {
  219.         $_[1] = 1;
  220.         return;
  221.       }
  222.       # Changed from 254 
  223. #     if ($_ > 255 ) {
  224. #       $_[1] = 2;
  225. #       return;
  226. #     }
  227.  
  228.       $targ = $targ * 256 + $_; 
  229.     }
  230.  
  231.     $targ;
  232.   }
  233. }
  234.  
  235. sub check_phone {
  236.   my $phone = $_[0];
  237. #  $phone =~ s/ //;
  238. #  !($phone =~ /^(\d*[!#])?(1?[\/\-.]?)?(\(?\d{3}[).\/\-]?)?\d{3}[.\/\-]?\d{4}(x\d{5,})?$/);
  239.  
  240.   $phone =~ s/[\(\)\-\,]//g;
  241.   $phone =~ /[^0-9]/o;
  242. }
  243.  
  244. sub get_fields {
  245.   my $buffer, $name, $value, $pair, @pairs;
  246.  
  247.   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  248.  
  249.   @pairs = split(/&/, $buffer);
  250.  
  251.   foreach $pair (@pairs) {
  252.     ($name, $value) = split(/=/, $pair);
  253.     $value =~ tr/+\t/  /;
  254.     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  255.         
  256.     $name = "xaster" if ($name eq "*");
  257.         
  258.     $fld{$name} = $value;
  259.   }
  260. }
  261.  
  262. sub check_ipaddr {
  263.   my $error;
  264.   return 1 if ($_[0] =~ /\.$/);
  265.   &convert_ipnum($_[0], $error);
  266.   $error != 0 ? 1 : 0; 
  267. }
  268.  
  269. sub check_netmask {
  270.   my $error, $count, $comp, $stop, $rarg;
  271.   my $arg = &convert_ipnum($_[0], $error);
  272.  
  273.   return 1 if $error;
  274.  
  275.   $rarg = 4294967296 - $arg;
  276.  
  277.   $count = 0;
  278.   $comp = 1;
  279.   while ($count < 33 && $stop == 0) {
  280.     if ($comp == $rarg) { $stop = 1; }
  281.     $comp *= 2;
  282.     $count++;
  283.   }
  284.   $stop ? 0 : 1;    
  285.  
  286. #  $arg = ($arg >> 1) & 0x7FFFFFF while !($arg & 1);
  287. #  $arg = ($arg >> 1) & 0x7FFFFFF while $arg & 1;
  288.  
  289. #  $arg ? 1 : 0;
  290. }
  291.  
  292. sub check_fname {
  293.   return "Filename $_[0] contains system metacharacters."
  294.       if $_[0] =~ /$METACHARS/o;
  295.   return "Filename $_[0] contains whitespace characters."  
  296.       if $_[0] =~ /\s/;
  297.   return "Filename $_[0] must be fully qualified."
  298.       if substr($_[0],0,1) ne "/";
  299.  
  300.   foreach $arg (@bad_filenames) {
  301.       return "$_[0] is a reserved directory." if $arg eq $_[0];
  302.   }
  303.  
  304.   if ($_[1]) {
  305.     if ($_[1] == 1) { 
  306.       if (! -e $_[0]) {
  307.         # This is stupid
  308.         # return "File $_[0] must already exist."
  309.         open (THE_FILE, "> $_[0]");
  310.         close (THE_FILE);
  311.     chmod 0644, $_[0];
  312.       }
  313.     }
  314.     if ($_[1] == 2) { 
  315.       return "File $_[0] must not currently exist."
  316.         if -e $_[0]; 
  317.     }
  318.     if ($_[1] == 3) { 
  319.       return "Directory $_[0] must already exist."
  320.         if ! -d $_[0]; 
  321.     }
  322.     if ($_[1] == 4) { 
  323.       return "Directory $_[0] must not currently exist."
  324.         if -e $_[0]; 
  325.     }
  326.   }
  327.  
  328.   return "";
  329. }
  330.  
  331. sub parse_list {
  332.     return split(/[, ]+/, $_[0]);
  333. }
  334.  
  335. sub check_int {
  336.   if ($_[0] =~ /^\s*(\d+)\s*$/) {
  337.  
  338.     # Min and Max
  339.     if ($_[1]) {
  340.       return 2 if $_[0] < $_[1];
  341.     }
  342.  
  343.     if ($_[2]) {
  344.       return 3 if $_[0] > $_[2];
  345.     }
  346.   
  347.     0;
  348.   }
  349.   else {
  350.     1;
  351.   };
  352. }
  353.  
  354. sub error_block {
  355.   print qq|<font size=4 color="dd0000">Error: $_[0]</font>|;
  356. }
  357.  
  358. sub title_block {
  359.   print "<html><title>$_[0]</title><body>\n";
  360. }
  361.  
  362. sub js_title_block {
  363.   print "<html><title>$_[0]</title>\n",
  364.         "<script language=\"JavaScript\">\n",
  365.         "<!--\n\n",
  366.         "$_[1]\n\n",
  367.         "// -->\n",
  368.         "</script></head>\n",
  369.         "<body>\n";
  370. }
  371.  
  372. sub header_block {
  373.   print "<center><h1>$_[0]</h1></center>\n";
  374. }
  375.  
  376. # $_[1] being 1 indicates that we need to check if the host exists
  377. # return 4 if it does exist and 0 if does not
  378.  
  379. sub check_hostname {
  380.   return 911 if $_[0] =~ /$METACHARS/o;
  381.  
  382.   (my $arg) = ($_[0] =~ /^\s*(\S*)\s*$/);
  383.   if ($arg =~  /^[a-zA-Z]\w*([.\-][a-zA-Z]\w*)*$/) {
  384.     return 4 if $_[1] == 1 && !gethostbyname($arg);
  385.     return 0;
  386.   }
  387.  
  388.   return 1;
  389. }
  390.  
  391. open(PIPE, "<expr> | ");
  392.  
  393. sub select {
  394.   local($sel) = $_[1];
  395.   local($op);
  396.   local($j) = 1;
  397.   local($ret) = "<select name=\"$_[0]\">\n";
  398.   while ($_[++$j] ne "") {
  399.     $op = $_[$j];
  400.     if ($sel eq $op) { $ret .= "<option selected> $op\n"; }
  401.     else { $ret .= "<option> $op\n"; }
  402.   }
  403.   $ret .= "</select>\n";
  404. }
  405.  
  406. sub text {
  407.   local($val) = "<input name=\"$_[0]\" ";
  408.   if ($_[1]) { $val .= "value=\"$_[1]\" "; }
  409.   if ($_[2]) { $val .= "size=$_[2]"; }
  410.   $val .= ">"; 
  411.   return $val;
  412. }
  413.  
  414. sub buttons {
  415.   local($ret) = 
  416.   qq|<center><table border=2 cellspacing=0 cellpadding=0 width=300>
  417.   <tr><th align=center><input type="submit" name="$_[0]" value="$_[1]">
  418.   <th align=center><input type="reset" value="Reset">
  419.   <th align=center><input type="submit" name="help" value="Help">
  420.   </tr></table></center></font>|;
  421. }
  422.  
  423. sub js_buttons {
  424.   local($ret) = 
  425.   qq|<center><table border=2 cellspacing=0 cellpadding=0 width=300>
  426.   <tr><th align=center><input type="submit" name="$_[0]" value="$_[1]" $_[2]>
  427.   <th align=center><input type="reset" value="Reset" $_[3]>
  428.   <th align=center><input type="submit" name="help" value="Help" $_[3]>
  429.   </tr></table></center></font>|;
  430. }
  431.  
  432. sub radio {
  433.   local($sel) = $_[1];
  434.   local($op);
  435.   local($ret) = "";
  436.   local($j) = 1;
  437.   while ($_[++$j] ne "") {
  438.     $op = $_[$j];
  439.     if ($sel eq $op) { $ret .= qq|<input type="radio" name="$_[0]" 
  440.       value="$op" checked>$op |; }
  441.     else { $ret .= qq|<input type="radio" name="$_[0]" value="$op">$op |; }
  442.   }
  443.   return $ret;
  444. }
  445.  
  446. sub redirect {
  447.   print "Content-type: text/html\n\n",
  448.         "<HTML><HEAD>",
  449.         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=$_[0]\">",
  450.         "</HEAD><BODY></BODY></HTML>";
  451. }
  452.  
  453.  
  454. # These functions are for the IP Screen scripts 
  455.  
  456. sub readFilterFile {
  457.   open(FILTER, "< $_[0]");
  458.   undef %rules;
  459.   while(<FILTER>) {
  460.     if(/^# /) {
  461.       $key = $_;
  462.       $key =~ s/^# //g;
  463.       chop($key);
  464.       next;
  465.     }
  466.     $rules{$key} .= $_;
  467.   }
  468.   close(FILTER);
  469.  
  470.   @subnets = split(/\n/,$rules{'Internal Subnets'});
  471.   $i = 0;
  472.   foreach $arg (@subnets) {
  473.     @terms = split(/\./, $arg);
  474.     if ($terms[1] eq '0' && $terms[2] eq '0' && $terms[3] eq '0') {
  475.         $subnet_mask[$i++] = "&0xff000000=$arg";
  476.     } elsif ($terms[2] eq '0' && $terms[3] eq '0') {
  477.         $subnet_mask[$i++] = "&0xffff0000=$arg";
  478.     } elsif ($terms[3] eq '0') {
  479.         $subnet_mask[$i++] = "&0xffffff00=$arg";
  480.     } else {
  481.         $subnet_mask[$i++] = "=$arg";
  482.     }
  483.   }
  484. }
  485.  
  486. sub readOutputFile {
  487.   open(FILTER, "< $_[0]");
  488.   undef %nrules;
  489.   while(<FILTER>) {
  490.     if(/^# /) {
  491.       $key = $_;
  492.       $key =~ s/^# //g;
  493.       chop($key);
  494.       next;
  495.     }
  496.     $nrules{$key} .= $_;
  497.   }
  498.   close(FILTER);
  499. }
  500.  
  501. sub writeFilterFile {
  502.   open(FILTER, "> $_[0]");
  503.   foreach $key (keys %rules) {
  504.     print FILTER "# $key\n";
  505.     print FILTER $rules{$key};
  506.   }
  507.   close(FILTER);
  508. }
  509.  
  510. sub addFilterFile {
  511.   if ($_[1] eq "") { delete $rules{$_[0]}; }
  512.   else { $rules{$_[0]} = $_[1]; }
  513. }
  514.  
  515. sub writeOutputFile {
  516.   open(FILTER, "> $_[0]");
  517.   foreach $key (keys %nrules) {
  518.     print FILTER "# $key\n";
  519.     print FILTER $nrules{$key};
  520.   }
  521.   close(FILTER);
  522. }
  523.  
  524. sub addOutputFile {
  525.   if ($_[1] eq "") { delete $nrules{$_[0]}; }
  526.   else { $nrules{$_[0]} = $_[1]; }
  527. }
  528.  
  529.  
  530. sub getInternalSubnets {
  531.     return "" if ($rules{'Internal Subnets'} eq "");
  532.     @subnets = split(/\n/,$rules{'Internal Subnets'});
  533.     return @subnets;
  534. }
  535.  
  536. sub my_goto {
  537.     print "Content-type: text/html\n\n";
  538.     print "<HTML><HEAD>";
  539.     print "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=$_[0]\">";
  540.     print "</HEAD><BODY></BODY></HTML>";
  541.     exit 0;
  542. }
  543.  
  544. sub processSubnet {
  545.         @terms = split(/\./, $_[0]);
  546.         if ($terms[1] eq '0' && $terms[2] eq '0' && $terms[3] eq '0') {
  547.                 return "&0xff000000=$_[0]";
  548.         } elsif ($terms[2] eq '0' && $terms[3] eq '0') {
  549.                 return "&0xffff0000=$_[0]";
  550.         } elsif ($terms[3] eq '0') {
  551.                 return "&0xffffff00=$_[0]";
  552.         } else {
  553.                 return "=$_[0]";
  554.         }
  555. }
  556.  
  557.  
  558. 1;
  559.